package cn.bouncyslime.huffmanTree.entity;

/**
 * 作者： 张一卓
 * 时间： 2019/12/31 11:07
 */
public class Node implements Comparable<Node> {
    /**
     * 权重，在赫夫曼树中指的是某一个字符的出现次数
     */
    private int weight;
    /**
     * 存放数据，字符
     */
    private Character data;
    /**
     * 左儿子节点
     */
    private Node leftNode;
    /**
     * 右儿子节点
     */
    private Node rightNode;
    /**
     * 该结点的huffman编码
     */
    private String code = "";

    public Node(Character data, int weight) {
        this.weight = weight;
        this.data = data;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Character getData() {
        return data;
    }

    public void setData(Character data) {
        this.data = data;
    }

    public Node getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(Node leftNode) {
        this.leftNode = leftNode;
    }

    public Node getRightNode() {
        return rightNode;
    }

    public void setRightNode(Node rightNode) {
        this.rightNode = rightNode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Node{" +
                "weight=" + weight +
                ", data='" + data + '\'' +
                ", coding='" + code + '\'' +
                '}';
    }

    /**
     * 当参数节点的weight大于该节点的weight，则返回1，等于则返回0，小于则返回-1
     * 这样在比较大小时，weight
     *
     * @param node
     * @return
     */
    @Override
    public int compareTo(Node node) {
        if (weight < node.getWeight()) {
            return 1;
        } else if (weight > node.getWeight()) {
            return -1;
        } else {
            return 0;
        }
    }
}
